﻿#include "net/http_server.cpp"

#include "gtest/gtest.h"
#include "log.h"
#include "module.h"
#include "library.h"
#include "utility.hpp"
#include "net/base_net.hpp"

#include "net/mqtt/acl_mqtt_server_adapter.h"
/*class TestModlePtr:public fast::BasePlugModule{

};*/
using json = nlohmann::json;
// 函数指针
TEST(modle, function_point) {

    try {
        //Library library;
        if (!fast::Singleton<Library>::GetInstance()->GetModule("./testDll.dll")) {
            LOG_INFO << "load fail";
        }

        // weaK_ptr 析构影响DestroySharedDll() shared_ptr
        {
            auto weak_ptr_module = fast::Singleton<Library>::GetInstance()->GetSource("TestModule");
            {
                auto shared_ptr = weak_ptr_module.lock();

                if (fast_not_null(shared_ptr)) {

                    auto ptr = shared_ptr.get();

                    json json2 = *ptr;
                    LOG_INFO << json2;

                }
            }
        }

        BasePlugModule plugModule;
        plugModule.m_version = " 版本号1 ";


        json json1 = plugModule;
        std::string json_string;
        json_string = json1.dump();
        LOG_INFO << json_string << json_string.size();
        LOG_INFO << json1;

        fast::Singleton<Library>::GetInstance()->DestroySharedDll();

    }
    catch (std::exception &e) {
        std::cout << e.what() << std::endl;
    }


}


TEST(modle, http_server) {
    auto const address = boost::asio::ip::make_address("127.0.0.1");
    auto const port = 80;
    auto const threads = 4;

    // The io_context is required for all I/O
    net::io_context ioc{threads};

    // Create and launch a listening port
    std::make_shared<listener>(ioc, tcp::endpoint{address, 8080})->run();

// Run the I/O service on the requested number of threads
    std::vector<std::thread> v;
    v.reserve(threads - 1);
    for (auto i = threads - 1; i > 0; --i) {
        v.emplace_back(
                [&ioc] {
                    ioc.run();
                });
    }
    ioc.run();
}


TEST(modle, base_net) {

/*    BaseNet baseNet;

    std::string networkName = R"({
      "NetType":"tcp"
    })";

    json net_worker_json = json::parse(networkName);*/



    std::thread thread_one([] {
        //Library library;
        if (!fast::Singleton<Library>::GetInstance()->GetModule("./testDll.dll")) {
            LOG_INFO << "load fail";
        }

    });

    std::thread thread_two([] {
        Sleep(1000);
        //Library library;
        auto weak_ptr_module = fast::Singleton<Library>::GetInstance()->GetSource("TestModule");
    });


    thread_one.detach();
    thread_two.detach();

    Sleep(99999);

}



//////////////////////////////////////////////////////////////////////////////

static void usage(const char *procname) {
    printf("usage: %s -h [help] -s addr\r\n", procname);
}


TEST(modle, acl_net) {
    char ch;
    acl::string addr("0.0.0.0|1883");


    acl::log::stdout_open(true);

    acl::aio_handle handle(acl::ENGINE_KERNEL);

    acl::aio_listen_stream *listener = new acl::aio_listen_stream(&handle);
    if (!listener->open(addr)) {
        printf("listen %s error %s\r\n", addr.c_str(), acl::last_serror());

    }
    printf("listen %s ok\r\n", addr.c_str());

    server_callback callback(handle);
    listener->add_accept_callback(&callback);

    while (handle.check()) { Sleep(500); }

    handle.check();
    listener->destroy();;
}